package com.tarena.lbs.user.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@Api(tags = "后台用户管理")
@RestController
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    JwtEncoder<UserPrinciple> jwtEncoder;
    @ApiOperation("用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail(@RequestHeader("Authorization") String token) throws BusinessException {
        log.debug("查询用户详情,controller层");
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException("-2", "缺少授权 Token");
        }
        try {
            UserPrinciple userPrinciple = jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
            if (userPrinciple == null) {
                throw new BusinessException("-2", "无效的 Token 或用户信息缺失");
            }
            AdminVO adminVO = adminService.detail(userPrinciple.getId());
            return new Result<>(adminVO);
        } catch (BusinessException e) {
            log.error("Token解析或用户信息获取失败: {}", e.getMessage());
            throw e;
        }
    }
    //查询后台管理的 账号列表
    @ApiOperation("账号列表")
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query)
        throws BusinessException{
        log.debug("查询账号列表,controller层");
        return new Result<>(adminService.pageList(query));
    }

    @ApiOperation("账号保存")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param)
        throws BusinessException{
        log.debug("账号保存,controller层{}",param);
        adminService.save(param);
        return Result.success();
    }













































}
